﻿using OperationPlugins;
using System.ComponentModel.Composition;
using TimeProject.Engine;
using TimeProject.Engine.DbContent;

namespace TimeProject.WebSite.Exports
{
    [Export(typeof(IOperationContextFactory))]
    [TimeProjectPriority]
    public class TimeProjectOperationContextFactory : IOperationContextFactory
    {
        /// <summary>
        /// Creates a new operation context.
        /// </summary>
        /// <param name="operation">Identifies the current operation.</param>
        /// <param name="branch">Identifies the current branch.</param>
        /// <returns>A new operation context</returns>
        OperationContext IOperationContextFactory.CreateOperationContext(OperationId operation, BranchId branch)
        {
            var context = new OperationContextImpl {Operation = operation, Branch = branch};
            return context;
        }

        private class OperationContextImpl : TimeProjectOperationContext
        {
            /// <summary>
            /// Gets the db context.
            /// </summary>
            public override TimeProjectDbContext Db
            {
                get { return RequestContext.Current.Db; }
            }

            /// <summary>
            /// Gets the current user.
            /// </summary>
            public override UserInfo CurrentUser
            {
                get { return RequestContext.Current.CurrentUser; }
            }
        }

    }
}